package org.dromara.authcode.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.authcode.domain.vo.AuthCodeVo;
import org.dromara.authcode.domain.bo.AuthCodeBo;
import org.dromara.authcode.service.IAuthCodeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 激活码
 *
 * @author Miles Meng
 * @date 2025-08-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/authcode/code")
public class AuthCodeController extends BaseController {

    private final IAuthCodeService authCodeService;

    /**
     * 查询激活码列表
     */
    @SaCheckPermission("authcode:code:list")
    @GetMapping("/list")
    public TableDataInfo<AuthCodeVo> list(AuthCodeBo bo, PageQuery pageQuery) {
        return authCodeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出激活码列表
     */
    @SaCheckPermission("authcode:code:export")
    @Log(title = "激活码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AuthCodeBo bo, HttpServletResponse response) {
        List<AuthCodeVo> list = authCodeService.queryList(bo);
        ExcelUtil.exportExcel(list, "激活码", AuthCodeVo.class, response);
    }

    /**
     * 获取激活码详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("authcode:code:query")
    @GetMapping("/{id}")
    public R<AuthCodeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(authCodeService.queryById(id));
    }

    /**
     * 新增激活码
     */
    @SaCheckPermission("authcode:code:add")
    @Log(title = "激活码", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AuthCodeBo bo) {
        return toAjax(authCodeService.insertByBo(bo));
    }

    /**
     * 修改激活码
     */
    @SaCheckPermission("authcode:code:edit")
    @Log(title = "激活码", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AuthCodeBo bo) {
        return toAjax(authCodeService.updateByBo(bo));
    }

    /**
     * 删除激活码
     *
     * @param ids 主键串
     */
    @SaCheckPermission("authcode:code:remove")
    @Log(title = "激活码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(authCodeService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 生成激活码
     */
    @SaCheckPermission("authcode:code:add")
    @Log(title = "激活码", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/generate")
    public R<Void> generate() {
        return toAjax(authCodeService.generateAuthCode());
    }
}
